package hong.jun.pet_home.controller.product;

import hong.jun.basic.util.AjaxResult;
import hong.jun.basic.util.PageList;
import hong.jun.pet_home.domain.product.Product;

import hong.jun.pet_home.query.ProductQuery;
import hong.jun.pet_home.service.product.IProductService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * restfull风格
 *
 * @Controller
 * @ResponseBody
 */

@RestController
@RequestMapping("/product")
@Api(tags = "ProductController", description = "ProductController | 服务管理接口")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 判断有无id决定新增还是修改
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Product product) {
        try {
            if (product.getId() != null) {
                productService.update(product);
            } else {
                productService.save(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    /**
     * 根据id 删除
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            //System.out.println(id);
            Product product = productService.getById(id);
            System.out.println("删除" + product.toString());//未查到对象为空会报空指针异常
            productService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    /**
     * 根据id 批量删除
     */
    @PostMapping("/dels")
    public AjaxResult dels(@RequestBody Long[] ids) {
        try {
            System.out.println(ids.toString());
            productService.deletes(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    /**
     * 根据id 批量上架
     */
    @PutMapping("/onsale")
    public AjaxResult onsale(@RequestBody Long[] ids) {
        try {
            productService.on_offsale(ids, 1);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    /**
     * 根据id 批量下架
     */
    @PutMapping("/offsale")
    public AjaxResult offsale(@RequestBody Long[] ids) {
        try {
            productService.on_offsale(ids, 0);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    /**
     * 根据Id查询
     */
    @GetMapping(value = "/{id}")
    public Product getById(@PathVariable("id") Long id) {
        return productService.getById(id);
    }

    /**
     * 查询上架的全部商品
     */
    @PostMapping("/getAll")
    public List<Product> getAll() {
        return productService.getAll();
    }

    /**
     * 条件分页查询
     */
    @PostMapping
    public PageList<Product> getPage(@RequestBody ProductQuery query) {
        System.out.println("条件" + query);
        return productService.queryPage(query);
    }
}
